// 与数据库打交道

const { Success, SysErr } = require('../../../response/code')
const { count: userCount } = require('../../../model/user')
const { count: logCount } = require('../../../model/log')

const { createRedisClient } = require('../../../util/redis')

const { USER_LIST_SORT_SET } = require('../../../consts/system')

class DashboardService {
  async info(ctx) {
    try {
      const data = ctx.request.body
      //
      const all = await userCount({ deleted_at: null })
      const conn = createRedisClient()
      await conn.connect()
      const online = await conn.zCount(`${USER_LIST_SORT_SET}`, '-inf', '+inf')
      await conn.close()
      const result = {
        user: {
          online: online,
          offline: all - online,
        },
        log: {
          all: await logCount({ deleted_at: null }),
          //
          today: await logCount({
            created_at: { gte: new Date(new Date().toLocaleDateString()) },
            deleted_at: null,
          }),
        },
      }

      // const result = await
      ctx.body = new Success(result).toString()
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }
}
module.exports = new DashboardService()
